package com.yangdada.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangdada.dto.*;
import com.yangdada.entity.Blog;
import com.yangdada.mapper.BlogMapper;
import com.yangdada.service.IBlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2022-08-06
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {
    @Resource
    private BlogMapper blogMapper;

    @Override
    public List<PublicDataDTO> getTwoDataTest(PublicRequestDTO publicRequestDTO) {
        List<PublicDataDTO> userNumList = blogMapper.getTwoDatauserNum(publicRequestDTO);
        List<PublicDataDTO> shopNumList = blogMapper.getTwoDataShopNum(publicRequestDTO);
        Map<String, Integer> collect = shopNumList.stream().collect(Collectors.toMap(PublicDataDTO::getDate, PublicDataDTO::getShopNum));
        for (PublicDataDTO publicDataDTO : userNumList) {
            Integer shopNum = collect.get(publicDataDTO.getDate());
            publicDataDTO.setShopNum(shopNum);
        }
        return userNumList;
    }

    @Override
    public TwoDataTestResponseDTO getTwoDataTestResponseData(PublicRequestDTO publicRequestDTO) {
        TwoDataTestResponseDTO twoDataTestResponseDTO = new TwoDataTestResponseDTO();
        Integer userNum = blogMapper.getTwoDataTestUserNumData(publicRequestDTO);
        Integer shopNum = blogMapper.getTwoDataTestShopNumData(publicRequestDTO);
        twoDataTestResponseDTO.setUserNum(userNum);
        twoDataTestResponseDTO.setShopNum(shopNum);
        return twoDataTestResponseDTO;
    }

    @Override
    public TwoDataResponseDTO getTwoData(PublicRequestDTO publicRequestDTO) {
        TwoDataResponseDTO userNumData = blogMapper.getUserNumData(publicRequestDTO);
        TwoDataResponseDTO shopNumData = blogMapper.getShopNumData(publicRequestDTO);
        userNumData.setShopNum(shopNumData.getShopNum());
        if (publicRequestDTO.getHourFlag() == 1){
            List<TwoDataMergeResponseDTO> listUserNum = blogMapper.getTwoMergeListHour(publicRequestDTO);
            List<TwoDataMergeResponseDTO> listShopNUm = blogMapper.getTwoMergeListShopHour(publicRequestDTO);
            Map<String, Integer> collect = listShopNUm.stream().collect(Collectors.toMap(TwoDataMergeResponseDTO::getDate, TwoDataMergeResponseDTO::getShopNum));
            for (TwoDataMergeResponseDTO twoDataMergeResponseDTO : listUserNum) {
                twoDataMergeResponseDTO.setShopNum(collect.get(twoDataMergeResponseDTO.getDate()));
            }
            userNumData.setTwoDataMergeResponseDTOList(listUserNum);
        }else{
            List<TwoDataMergeResponseDTO> listUserNum = blogMapper.getTwoMergeList(publicRequestDTO);
            List<TwoDataMergeResponseDTO> listShopNUm = blogMapper.getTwoMergeListShop(publicRequestDTO);
            Map<String, Integer> collect = listShopNUm.stream().collect(Collectors.toMap(TwoDataMergeResponseDTO::getDate, TwoDataMergeResponseDTO::getShopNum));
            for (TwoDataMergeResponseDTO twoDataMergeResponseDTO : listUserNum) {
                twoDataMergeResponseDTO.setShopNum(collect.get(twoDataMergeResponseDTO.getDate()));
            }
            userNumData.setTwoDataMergeResponseDTOList(listUserNum);
        }
        return userNumData;
    }
}
